package com.jerry.secondkill.service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jerry.secondkill.dao.UserDao;
import com.jerry.secondkill.domain.User;
import com.jerry.secondkill.exception.GlobalException;
import com.jerry.secondkill.redis.RedisService;
import com.jerry.secondkill.redis.key.UserKey;
import com.jerry.secondkill.result.CodeMsg;
import com.jerry.secondkill.util.MD5Util;
import com.jerry.secondkill.util.UUIDUtil;
import com.jerry.secondkill.vo.LoginVo;

@Service
public class UserService {
	
	public static final String COOKI_NAME_TOKEN = "token";
	
	@Autowired
	UserDao userDao;
	@Autowired
	RedisService redisService;
	
	/**
	 * 此处处理对象级缓存
	 * @param id
	 * @return
	 */
	public User getById(long id) {
		//取缓存
		User user = redisService.get(UserKey.getById, ""+id, User.class);
		if(user != null) {
			return user;
		}
		//取数据库
		user = userDao.getById(id);
		if(user != null) {
			//向缓存中写数据
			redisService.set(UserKey.getById, ""+id, user);
		}
		return user;
	}
	
	public boolean updatePassword(String token, long id, String formPass) {
		//取user
		User user = getById(id);
		if(user == null) {
			throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
		}
		//更新数据库
		User updateUser = new User();
		updateUser.setId(id);
		updateUser.setPassword(MD5Util.formPassToDBPass(formPass, user.getSalt()));
		userDao.update(updateUser);
		//处理缓存
		//注意：对象级缓存，数据对象有更新时，一定要及时更新对象的缓存数据
		redisService.delete(UserKey.getById, ""+id);
		user.setPassword(updateUser.getPassword());
		redisService.set(UserKey.token, token, user);
		return true;
	}
	
	public User getByToken(HttpServletResponse response, String token) {
		if(StringUtils.isEmpty(token)) {
			return null;
		}
		User user = redisService.get(UserKey.token, token, User.class);
		//延长有效期
		if(user != null) {
			addCookie(response, token, user);
		}
		return user;
	}
	
	public boolean login(HttpServletResponse response, LoginVo loginVo) {
		if(loginVo == null) {
			throw new GlobalException(CodeMsg.SERVER_ERROR);
		}
		String mobile = loginVo.getMobile();
		String formPass = loginVo.getPassword();
		//判断手机号是否存在
		User user = getById(Long.parseLong(mobile));
		if(user == null) {
			throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
		}
		//验证密码
		String dbPass = user.getPassword();
		String saltDB = user.getSalt();
		String calcPass = MD5Util.formPassToDBPass(formPass, saltDB);
		if(!calcPass.equals(dbPass)) {
			throw new GlobalException(CodeMsg.PASSWORD_ERROR);
		}
		//生成token
		String token	 = UUIDUtil.uuid();
		//token保存到cookie中
		addCookie(response, token, user);
		return true;
	}
	
	private void addCookie(HttpServletResponse response, String token, User user) {
		//token保存到redis中
		redisService.set(UserKey.token, token, user);
		Cookie cookie = new Cookie(COOKI_NAME_TOKEN, token);
		cookie.setMaxAge(UserKey.token.expireSeconds()); //设置cookie的有效期
		cookie.setPath("/");
		response.addCookie(cookie);
	}

}
